import QrcodeCard from './card.js'
var app = getApp()
Page({
  data: {
    // canvas
    bg: './images/bg.png',
    head: './images/dad.png',
    canvasId: 'baby-qrcode',
    canvasWidth: '',
    canvasHeight: '',
    qrcode: '', // 二维码
    // canvas

    code: '13081239423',
    copied: false,
    form: {
      childUserId: '',
      relationType: 2
    },
    childInviteCode: '',
    options: null
  },
  onLoad(options){
    var form = this.data.form
    form.childUserId = options.childId
    this.setData({
      options,
      form
    })
    var _this = this
    wx.getImageInfo({
      src: _this.data.options.avator,
      success(res) {
        _this.data.head = res.path
      }
    })
    
    this.getBabyQrcode()
  },
  getBabyQrcode(){
    var _this = this
    app.middleware.requestAjax({
      serAddr: app.serAddrs['getBabyQrcode'],
      data: this.data.form,
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        _this.setData({
          ewmUrl: res.data.ewmUrl,
          childInviteCode: res.data.childInviteCode
        })

        wx.getImageInfo({
          src: res.data.ewmUrl,
          success(res) {
            _this.data.qrcode = res.path
            _this.data.cardCanvas = _this.initCard()
          }
        })
      }
    })
  },

  // 初始化canvas
  initCard() {
    var _this = this
    var { options, bg, qrcode, head, canvasId } = _this.data
    var cardCanvas = new QrcodeCard({
      canvasId,
      bg,
      head,
      qrcode,
      updateCanvasSize(res) {
        _this.setData(res)
      }
    }).init()
    return cardCanvas
  },
  saveImage(e){
    const _this = this
    wx.getSetting({
      success(res) {
        // 已授权
        if (res.authSetting['scope.writePhotosAlbum'] === true) {
          wx.showLoading({
            title: '正在保存图片'
          })
          // _this.saveCard()
          // var { bg, qrcode, canvasId,options,head } = _this.data
          
          // new QrcodeCard({
          //   canvasId,
          //   bg,
          //   head,
          //   qrcode,
          //   updateCanvasSize(res) {
          //     _this.setData(res)
          //   }
          // }).init().saveCard()
          _this.data.cardCanvas.saveCard()

          // 先下载图片，再保存到相册
          // wx.downloadFile({
          //   url: _this.data.ewmUrl,
          //   success(res){
          //     // 将本地缓存图片保存到相册
          //     wx.saveImageToPhotosAlbum({
          //       filePath: res.tempFilePath,
          //       success(res){
          //         wx.hideLoading()

          //         wx.showToast({
          //           title: '图片保存成功',
          //           icon: 'none'
          //         })
          //       },
          //       fail(err){
          //         wx.hideLoading()

          //         wx.showToast({
          //           title: '图片保存失败',
          //           icon: 'none'
          //         })
          //       }
          //     })
          //   },
          //   fail(){
          //     wx.hideLoading()

          //     wx.showToast({
          //       title: '图片保存失败',
          //       icon: 'none'
          //     })
          //   }
          // })
        }
        // 拒绝授权
        else if (res.authSetting['scope.writePhotosAlbum'] === false) {
          wx.openSetting({
            success(res){
              // console.log(res)
            }
          })
        }
        // 尚未请求用户授权
        else if (res.authSetting['scope.writePhotosAlbum'] === undefined) {
          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success(res){
              console.log('用户主动授权',res)
            },
            fail(err) {
              console.log(err, '不收钱')
            }
          })
        }
      }
    })
  },
  copyCode(){
    const _this=this
    wx.setClipboardData({
      data: this.data.childInviteCode,
      success(){
        _this.setData({
          copied: true
        })    
      }
    })
  },
  onShareAppMessage(res){
    if (res.from == 'button') {
      console.log(res)
    }
    var {childId,avator,childName} = this.data.options
    return {
      title: '邀请关注',
      path: app.xhx_route.babyInvite+`?childId=${childId}&avator=${avator}&name=${childName}`,
      imageUrl: './images/share.png'
    }
  },
  
})
